$(function () {

   /* //搜索框赋值
    if (localStorage.getItem('search')!=null){
        console.log(localStorage.getItem('search'));
        $('#search-text').val(localStorage.getItem('search'))
        searchProduct()
    }*/



    //品牌栏
    $('#navigation-crumbSlide').on("DOMNodeInserted",function () {
        $.get('/eshop/brand.do?m=findByBrandType',{productTypeName:$(this).text()},function (res) {
            $('.brands>ul>li').remove();
            console.log(res);
            res.forEach(function (e,i) {
                $(".brands>ul").append(' <li><input type="checkbox" class="chk" name="chk" value="'+e.id+'" id="chk'+(i+1)+'"/> <label for="chk'+(i+1)+'"><img src="/eshop/Img.do?m=showImg&name='+e.brandImg+'" width="50" height="50px"/><br />'+e.brandName+' <img src="img/choose.PNG" class="choose"/> </label> </li>')
            })
        },'json')

        subClassify()
    })

    $("#navigation-crumbSlide").text(localStorage.getItem("productTypeName"))

    subClassify();


})

function renderingBrand(str) {
    $("#navigation-crumbSlide").text(str)
}

function subClassify(order) {
    let arr='';
    $("input[name='chk']").each(function () {
        let state = $(this).prop('checked');
        if (state){
            arr+=$(this).val()+","
        }
    })
    if (arr!=""){
        arr = arr.substr(0,arr.length-1);
    }
    $.ajax({
        type:'get',
        url:'/eshop/product.do?m=findProducts',
        data:{
            typeName:$('#navigation-crumbSlide').text(),
            args:arr,
            keyWord:$("#search-text").val().length<=0?null:$("#search-text").val(),
            orderBy:order==null?null:order,
            min:$($("input[name='min']")[0]).val(),
            max:$($("input[name='max']")[0]).val()
        },
        success:function (data) {
            $("#goods_product>li").remove();
            data.forEach(function (e,i) {
                $("#goods_product").append(' <li><a href=javascript:void(0)" onclick="toGoodsDetail(\''+e.id+'\')">\n' +
                    '            <div class="g-img"><img src="/eshop/Img.do?m=showImg&name='+e.productImage+'" alt="" width="230px" height="230px"/></div>\n' +
                    '            <p class="g-title">'+e.productName+'</p>\n' +
                    '            <span class="g-price">￥'+e.price+'</span>\n' +
                    '            <span class="g-num">销量:'+e.productNum+'</span>\n' +
                    '        </a></li>')
            })

        }
    })

}

function searchProduct() {
    $(".chk").attr("checked",false);
    $(".price").attr("value","");
    $('#navigation-crumbSlide').text('')
    $('.brands>ul>li').remove();
    $.ajax({
        type:'get',
        url:'/eshop/product.do?m=findProducts',
        data:{
            keyWord:$("#search-text").val()
        },
        success:function (data) {
            $("#goods_product>li").remove();
            data.forEach(function (e,i) {
                $("#goods_product").append(' <li><a href=javascript:void(0)" onclick="toGoodsDetail(\''+e.id+'\')">\n' +
                    '            <div class="g-img"><img src="/eshop/Img.do?m=showImg&name='+e.productImage+'" alt="" width="230px" height="230px"/></div>\n' +
                    '            <p class="g-title">'+e.productName+'</p>\n' +
                    '            <span class="g-price">￥'+e.price+'</span>\n' +
                    '            <span class="g-num">销量:'+e.productNum+'</span>\n' +
                    '        </a></li>')
            })

        }
    })
}